package com.ht.demo.admin.controller.system;


import com.ht.demo.common.appexception.AppException;
import com.ht.demo.common.appexception.ResultData;
import com.ht.demo.common.enums.OperateTypeEnum;
import com.ht.demo.common.pojo.bo.system.SysDeptBO;
import com.ht.demo.common.pojo.qto.system.SysDeptQTO;
import com.ht.demo.common.pojo.vo.TreeSelect;
import com.ht.demo.service.annotation.LoggerInfo;
import com.ht.demo.service.annotation.Permission;
import com.ht.demo.service.manager.system.SysDeptManager;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("${basePath}")
public class SysDeptController {
    @Resource
    private SysDeptManager sysDeptManager;

    @Permission(permissionId = "system:dept:add", desc = "部门新增")
    @LoggerInfo(title = "部门管理", operateType = OperateTypeEnum.INSERT)
    @PostMapping("/system/dept/insert")
    public ResultData<Boolean> saveDept(@RequestBody SysDeptBO dept) throws AppException {
        return new ResultData<>(sysDeptManager.saveDept(dept));
    }

    @Permission(permissionId = "system:dept:edit", desc = "部门修改")
    @LoggerInfo(title = "部门管理", operateType = OperateTypeEnum.UPDATE)
    @PutMapping("/system/dept/update")
    public ResultData<Boolean> updateDept(@RequestBody SysDeptBO dept) throws AppException {
        return new ResultData<>(sysDeptManager.updateDept(dept));
    }

    @Permission(permissionId = "system:dept:remove", desc = "部门删除")
    @LoggerInfo(title = "部门管理", operateType = OperateTypeEnum.DELETE)
    @DeleteMapping("/system/dept/delete/{sysDeptId}")
    public ResultData<Boolean> delDept(@PathVariable Long sysDeptId) throws AppException {
        return new ResultData<>(sysDeptManager.delDept(sysDeptId));
    }

    @GetMapping("/system/dept/findDeptById/{sysDeptId}")
    public ResultData<SysDeptBO> findDeptById(@PathVariable Long sysDeptId) throws AppException {
        return new ResultData<>(sysDeptManager.findDeptById(sysDeptId));
    }

    @GetMapping("/findSysDeptList")
    public ResultData<List<SysDeptBO>> findSysDeptList(SysDeptQTO qto) throws AppException {
        List<SysDeptBO> deptList = sysDeptManager.findSysDeptList(qto);
        return new ResultData<>(deptList);
    }

    /**
     * 排除当前部门的列表
     * @param sysDeptId
     * @return
     * @throws AppException
     */
    @GetMapping("/system/dept/list/exclude/{sysDeptId}")
    public ResultData<List<SysDeptBO>> findSysDeptList(@PathVariable Long sysDeptId) throws AppException {
        List<SysDeptBO> deptList = sysDeptManager.findSysDeptList(new SysDeptQTO());
        deptList.removeIf(d -> d.getSysDeptId().intValue() == sysDeptId || ArrayUtils.contains(StringUtils.split(d.getAncestors(), ","), sysDeptId + ""));

        return new ResultData<>(deptList);
    }

    @GetMapping("/system/dept/list")
    public ResultData<List<SysDeptBO>> findSysDeptTree(SysDeptQTO qto) throws AppException {
        List<SysDeptBO> boList = sysDeptManager.findSysDeptList(qto);
        List<SysDeptBO> tree = sysDeptManager.buildDeptTree(boList);
        return new ResultData<>(tree);
    }

    @PostMapping("/findSysDeptTree")
    public ResultData<List<TreeSelect>> findSysDeptTreeSelect(@RequestBody SysDeptQTO qto) throws AppException {
        List<SysDeptBO> deptList = sysDeptManager.findSysDeptList(qto);
        List<SysDeptBO> deptTree = sysDeptManager.buildDeptTree(deptList);

        return new ResultData<>(sysDeptManager.buildTreeSelect(deptTree));
    }


}
